package xpn.business.fund_return.entity.assistance_return_notice;

import org.springframework.context.ApplicationContextException;
import org.springframework.stereotype.Service;
import xpn.business.fund_return.entity.frcase.FrCase;
import xpn.business.fund_return.entity.frcase.FrCaseDao;
import xpn.business.fund_return.entity.frcase.valueobject.CaseStatusCommon;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class AssistanceReturnNoticeService {
    @Resource
    private AssistanceReturnNoticeDao assistanceReturnNoticeDao;

//    @Resource
//    private FrCaseService frCaseService;

    @Resource
    private FrCaseDao frCaseDao;

    public AssistanceReturnNotice save(AssistanceReturnNotice assistanceReturnNotice) {
        if(Objects.isNull(assistanceReturnNotice.getId())) {
            AssistanceReturnNotice exist = assistanceReturnNoticeDao.findAllByCaseId(assistanceReturnNotice.getCaseId());
            if(Objects.nonNull(exist)) {
                throw new ApplicationContextException("该表单已存在，请勿重复创建");
            }
        }
        FrCase frCase = frCaseDao.getOne(assistanceReturnNotice.getCaseId());
        frCase.setCaseStateCommon(CaseStatusCommon.ProcessEnd);
        frCaseDao.save(frCase);
        //提出公共字段保存至FrCase
        return assistanceReturnNoticeDao.save(assistanceReturnNotice);
    }
}
